<template>
  <a-modal
    :open="open"
    width="680px"
    :title="rowData?.id ? '修改大事记' : '新增大事记'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="类型" class="w-1/2">
        <a-select v-model:value="formState.memoirType" placeholder="请选择">
          <a-select-option value="重大事件">重大事件</a-select-option>
          <a-select-option value="群体事件">群体事件</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="发生日期" class="w-1/2">
        <a-date-picker v-model:value="formState.memoirTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="主题" class="w-full">
        <a-input v-model:value="formState.subject" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="相关人员" class="w-full">
        <a-input v-model:value="formState.contactPerson" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="事件描述" class="w-full">
        <a-textarea v-model:value="formState.description" :rows="3" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="处理结果" class="w-full">
        <a-textarea v-model:value="formState.result" :rows="2" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="1" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  memoirType: undefined,
  subject: '',
  contactPerson: '',
  description: '',
  result: '',
  memo: ''
})
const rulesState = ref({})
const { isCompleted, resetFields, validate } = useForm(formState, rulesState)

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id) {
        formState.value = props.rowData
      } else {
        formState.value.memoirTime = dayjs().format('YYYY-MM-DD')
      }
    }
  }
)

const onCancel = () => {
  emit('update:open', false)
  resetFields()
  formState.value = {
    memoirType: undefined,
    subject: '',
    contactPerson: '',
    description: '',
    result: '',
    memo: ''
  }
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/war/warMemoir/doSaveWarMemoir', {
    ...formState.value
  })
  if (code === 0) {
    message.success('保存成功')
    onCancel()
    emit('ok')
  } else {
    message.error(msg)
  }
}
</script>
